{% extends "base.html" %}
{% block header-content %}
<h3>Session</h3>
<p>Session is a class to manage persistent data across multiple page views. It includes user IP and User Agent validation, as well as session token regeneration to manage session fixation vulnerabilities. Only the session id is stored via cookie, all other data is stored on server side. Session data can be set using standard Python container access methods (Session()["key"] = "value").
</p>
{% endblock %}
{% block content %}
    {% if sess.flash %}
    <p>{{ sess.flash }}</p>
    {% endif %}
    <p> Welcome to the sessions demo. You've viewed this page <strong>{{ sess.viewCount }}</strong> times during this session. Your session id is <strong>{{ sess.sid }}</strong> There are {{ session_length }} objects in your session data.
<table><tr>
<ul>
<td><b>iteration test</b><br />
{% for k in sess %}
<li>{{ k }}</li>
{% endfor %}
</td><td><b>items test</b><br />
{% for i in sess.items %}
<li>{{ i }}</li>
{% endfor %}
</td><td><b>keys test</b><br />
{% for k in sess.keys %}
<li>{{ k }}</li>
{% endfor %}
</td><td><b>values test</b><br />
{% for v in sess.values %}
<li>{{ v }}</li>
{% endfor %}
</td></tr></table>
When the Django template library is upgraded this demo can be modified to show the values as well. For now this is enough to demonstrate iteration support.
</ul><br />
                You can <a href="/session">View this page again</a>, you can <a href="/session?setflash=true">Set flash data</a>, pr you can <a href="/session?deleteSession=true">Delete your session</a> and start over.</p>
<p>Memcache stats: {{ memcacheStats }}</p>
<p>Session str: {{ sess_str }}</p>
  <div id="container"></div>
  <div><input type="button" id="requestButton" value="Get 1" /><input type="button" id="requestButton10" value="Get 10" /><input type="button" id="requestButton20" value="Get 20" /></div>

<div class="code">
<h2>webapp class</h2>
<pre>{% filter escape %}class SessionPage(webapp.RequestHandler):
class SessionPage(webapp.RequestHandler):
  def get(self):
    self.sess = sessions.Session()
    if self.request.get('deleteSession') == "true":
        self.sess.delete()
        print "Location: /session\n\n"
    elif self.request.get('setflash') == "true":
        self.sess['flash'] = 'You set a flash message! <a href="/session">Refresh this page</a> and this message is gone!'
        print "Location: /session\n\n"
    else:
        self.sess['testKey'] = "test"
        self.sess['testKey2'] = "test2"
        if not 'viewCount' in self.sess:
            self.sess['viewCount'] = 1
        else:
            self.sess['viewCount'] = int(self.sess['viewCount']) + 1
        self.memcacheStats = memcache.get_stats()
        template_values = {
            'sess': self.sess,
            'memcacheStats': self.memcacheStats
        }
        path = os.path.join(os.path.dirname(__file__), 'templates/session.html')
        self.response.out.write(template.render(path, template_values))
{% endfilter %}</pre>
</div>
<div class="code">
<h2>template</h2>
<pre>
{% filter escape %}    {% templatetag openblock %}{% templatetag closeblock %} if sess.flash {% templatetag closeblock %}
    <p>{% templatetag openvariable %} sess.flash {% templatetag closevariable %}</p>
    {% templatetag openblock %}{% templatetag closeblock %} endif {% templatetag closeblock %}
    <p> Welcome to the sessions demo. You've viewed this page <strong>{% templatetag openvariable %} sess.viewCount {% templatetag closevariable %}</strong> times during this session. Your session id is <strong>{% templatetag openvariable %} sess.sid {% templatetag closevariable %}</strong> There are {% templatetag openvariable %} session_length {% templatetag closevariable %} objects in your session data.
<table><tr>
<ul>
<td><b>iteration test</b><br />
{% templatetag openblock %}{% templatetag closeblock %} for k in sess {% templatetag closeblock %}
<li>{% templatetag openvariable %} k {% templatetag closevariable %}</li>
{% templatetag openblock %}{% templatetag closeblock %} endfor {% templatetag closeblock %}
</td><td><b>items test</b><br />
{% templatetag openblock %}{% templatetag closeblock %} for i in sess.items {% templatetag closeblock %}
<li>{% templatetag openvariable %} i {% templatetag closevariable %}</li>
{% templatetag openblock %}{% templatetag closeblock %} endfor {% templatetag closeblock %}
</td><td><b>keys test</b><br />
{% templatetag openblock %}{% templatetag closeblock %} for k in sess.keys {% templatetag closeblock %}
<li>{% templatetag openvariable %} k {% templatetag closevariable %}</li>
{% templatetag openblock %}{% templatetag closeblock %} endfor {% templatetag closeblock %}
</td><td><b>values test</b><br />
{% templatetag openblock %}{% templatetag closeblock %} for v in sess.values {% templatetag closeblock %}
<li>{% templatetag openvariable %} v {% templatetag closevariable %}</li>
{% templatetag openblock %}{% templatetag closeblock %} endfor {% templatetag closeblock %}
</td></tr></table>
When the Django template library is upgraded this demo can be modified to show the values as well. For now this is enough to demonstrate iteration support.
</ul><br />
                You can <a href="/session">View this page again</a>, you can <a href="/session?setflash=true">Set flash data</a>, pr you can <a href="/session?deleteSession=true">Delete your session</a> and start over.</p>
<p>Memcache stats: {% templatetag openvariable %} memcacheStats {% templatetag closevariable %}</p>
<p>Session str: {% templatetag openvariable %} sess_str {% templatetag closevariable %}</p>
  <div id="container"></div>
  <div><input type="button" id="requestButton" value="Get 1" /><input type="button" id="requestButton10" value="Get 10" /><input type="button" id="requestButton20" value="Get 20" /></div>

{% endfilter %}</pre>
</div>
<script type="text/javascript">
YUI({base:"/yui/build/", timeout: 10000}).use("io",

    function(Y) {

        //Get a reference to the Node that we are using
        //to report results:
        var div = Y.Node.get('#container');

        //A function handler to use for successful requests:
        var handleSuccess = function(ioId, o){
            Y.log(arguments);
            Y.log("The success handler was called.  Id: " + ioId + ".", "info", "example");

            if(o.responseText !== undefined){
                var s = "<li>Transaction id: " + ioId + "</li>";
                s += "<li>HTTP status: " + o.status + "</li>";
                s += "<li>Status code message: " + o.statusText + "</li>";
                s += "<li>HTTP headers received: <ul>" + o.getAllResponseHeaders() + "</ul></li>";
                s += "<li>response: " + o.responseText + "</li>";
                div.set("innerHTML", s);
            }
        };

        //A function handler to use for failed requests:
        var handleFailure = function(ioId, o){
            Y.log("The failure handler was called.  Id: " + ioId + ".", "info", "example");

            if(o.responseText !== undefined){
                var s = "<li>Transaction id: " + ioId + "</li>";
                s += "<li>HTTP status: " + o.status + "</li>";
                s += "<li>Status code message: " + o.statusText + "</li>";
                div.set("innerHTML", s);
            }
        };

        //Subscribe our handlers to IO's global custom events:
        Y.on('io:success', handleSuccess);
        Y.on('io:failure', handleFailure);


        /* Configuration object for POST transaction */

        //The URL of the resource to which we're POSTing data:
        var sUrl = "/ajaxsession";

        //Handler to make our XHR request when the button is clicked:
        function makeRequest(){

            div.set("innerHTML", "Loading data from new request...");

            var request = Y.io(sUrl);
            Y.log("Initiating request; Id: " + request.id + ".", "info", "example");

        }

        function makeRequest10(){

            for(i=0; i<10; ++i)
            {
                div.set("innerHTML", "Loading data from new request...");
    
                var request = Y.io(sUrl);
                Y.log("Initiating request; Id: " + request.id + ".", "info", "example");
            }
        }


        function makeRequest20(){

            for(i=0; i<20; ++i)
            {
                div.set("innerHTML", "Loading data from new request...");
    
                var request = Y.io(sUrl);
                Y.log("Initiating request; Id: " + request.id + ".", "info", "example");
            }
        }

              
        // Make a request when the button is clicked:
        Y.on("click", makeRequest, "#requestButton");
        Y.on("click", makeRequest10, "#requestButton10");
        Y.on("click", makeRequest20, "#requestButton20");

        Y.log("As you interact with this example, relevant steps in the process will be logged here.", "info", "example");
    }
);
</script>
{% endblock %}
